<template>
  <PerfectTableComponent
    :loadTableList="loadTableList"
    :changePageSize="changePageSize"
    :changePage="changePage"
    :removeQueryField="removeQueryField"
    :clearQuery="clearQuery"
    v-model:query="query"
    title="物料批次"
    ref="tableRef"
    :queryFormHeaders="queryFormHeaders"
    :table="table"
    :tableColumns="tableColumns"
    :showTableIndex="false"
    :operatorColumnWidth="300"
    :baseUrl="urls.API_URL_BACKSTAGE_WMS_WMSMATERIALINVENTORY"
    :showDeleteButton="false"
    v-model:form="form"
    :formHeader="formHeader"
    @beforeAddRow="beforeAddRow"
    @afterLoadDataSuccessInEditRow="afterLoadDataSuccessInEditRow"
    @beforeFormSubmit="beforeFormSubmit"
  >
    <!-----------------------------表格自定义列----------------------------------------->
    <!--所属栏目1-->
    <template #table_column_materialName="scope: any">
      {{ scope.row.material != null ? scope.row.material.name : '' }}
    </template>
    <template #table_column_materialTypeName="scope: any">
      {{ scope.row.material != null ? scope.row.material.materialType.name : '' }}
    </template>
    <!-----------------------------表格自定义列（操作列）----------------------------------------->
    <template #table_operator_left="scope">
      <el-button
        @click="
          router.push({
            path: '/backstage/wms/MaterialInventoryDetail',
            query: {
              materialInventoryId: scope.row.id
            }
          })
        "
        type="primary"
        size="small"
        icon="Box"
        plain
        >库存管理</el-button
      >
    </template>
  </PerfectTableComponent>
</template>

<script setup lang="ts">
import PerfectTableComponent from '@/components/base/table/PerfectTableComponent.vue';
import { onMounted, provide, reactive, ref } from 'vue';
import useTableList from '@/api/hooks/useTableList';
import type { FormHeader, ResponseData, TableColumn } from '@/types/interface/base_interface';
import urls from '@/api/request/urls';
import { getDict } from '@/api/dict';
import type { MaterialBatch, WmsMaterialInventory } from '@/types/interface/wms_interface';
import Request from '@/api/request/request';
import { useRouter } from 'vue-router';
// 导入路由
const router = useRouter(); // 创建路由对象

// 表格工具必备参数
const { table, query, loadTableList, removeQueryField, clearQuery, changePageSize, changePage } = useTableList(
  urls.API_URL_BACKSTAGE_WMS_WMSMATERIALINVENTORY
);
// 将表格刷新函数传到子容器
provide('loadTableList', loadTableList);

//查询==========================================================
const queryFormHeaders = ref<FormHeader[]>([]);

//表格==========================================================
// 表格插件ref对象1
const tableRef = ref();

//表格列
// 定义表格所有的列字段信息
const tableColumns = ref<TableColumn[]>([]);

//表单==========================================================
//表单对象
const form = ref<WmsMaterialInventory>({});
//表单元素类型
const formHeader = ref<FormHeader[]>([]);

const getMaterialType = async () => {
  const resData: any = await new Request(urls.API_URL_BACKSTAGE_MES_MATERIALTYPE + '/all').get();
  if (resData.success) {
    return resData.data;
  }
};
onMounted(async () => {
  //定义优先级选项。放在mounted中执行时，要将引用改变量的属性也放进来。
  const lockDict = await getDict('锁定状态', 'boolean');
  //定义物料类别选项。放在mounted中执行时，要将引用改变量的属性也放进来。
  const typeDict = await getDict('物料类型', 'number');
  //获取物料类别列表
  const materialTypes = await getMaterialType();
  queryFormHeaders.value = [
    // 查询表单
    {
      prop: 'material.name',
      label: '物料名称'
    },
    {
      label: '物料类型',
      prop: 'material.type',
      type: 'select',
      list: typeDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      prop: 'materialTypeId',
      propAsName: 'material.materialType.id',
      label: '物料类别',
      type: 'selectObject',
      list: materialTypes,
      config: {
        valueKey: 'id',
        defaultProp: {
          value: 'id',
          label: 'name'
        }
      }
    },
    {
      prop: 'isLock',
      label: '是否锁定',
      type: 'select',
      list: lockDict
    }
  ];
  //表格对象
  tableColumns.value = [
    {
      label: '物料名',
      prop: 'materialName',
      type: 'custom',
      width: 200,
      fixed: true
    },
    {
      label: '物料类别',
      prop: 'materialTypeName',
      type: 'custom',
      width: 150,
      fixed: true
    },
    {
      label: '总库存',
      prop: 'totalInventory',
      width: 100,
      fixed: true
    },
    {
      label: '库存上限',
      prop: 'inventoryLimit',
      width: 130,
      fixed: true
    },
    {
      label: '库存下限',
      prop: 'inventoryLower',
      width: 100
    },
    {
      label: '锁定状态',
      prop: 'isLock',
      width: 100,
      type: 'dict',
      dict: lockDict
    },
    {
      label: '锁定数量',
      prop: 'lockQuantity',
      width: 100
    },
    {
      label: '库位数量',
      prop: 'seatNumber',
      width: 130
    }
  ];
  // 添加编辑表单
  formHeader.value = [
    {
      label: '物料',
      prop: 'materialId',
      propLabel: 'materialName',
      type: 'material',
      action: 'post',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '物料',
      prop: 'materialId',
      propLabel: 'materialName',
      type: 'material',
      action: 'put',
      readonly: true
    },
    {
      label: '库存上限',
      prop: 'inventoryLimit',
      type: 'number',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '库存下限',
      prop: 'inventoryLower',
      type: 'number',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '锁定状态',
      prop: 'isLock',
      type: 'select',
      list: lockDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '库位数量',
      prop: 'seatNumber',
      type: 'label',
      action: 'put'
    },
    {
      label: '锁定数量',
      prop: 'lockQuantity',
      type: 'label',
      action: 'put'
    }
  ];
});
//在添加行的动作以前
const beforeAddRow = async () => {
  //传入初始值
  form.value = {
    isLock: false
  };
};
//在编辑行读取后端记录后需要修改的地方
const afterLoadDataSuccessInEditRow = async (formEdit: WmsMaterialInventory) => {
  //重新封装表单对象的值
  if (formEdit.material != null) {
    formEdit.materialId = formEdit.material.id;
    formEdit.materialName = formEdit.material.name;
  }
};
//在表单提交前对对象进行封装
const beforeFormSubmit = () => {
  if (form.value.materialId != null) {
    form.value.material = {
      id: form.value.materialId
    };
  }
};
</script>

<style scoped></style>
